/**
 * Created by zyy on 15/9/8.
 * zhangyuyu@superjia.com
 */
var router = require('koa-router')({
    prefix: '/api'
});
var wrap = require('co-monk')
var parse = require('co-body')
var uuid = require('node-uuid')

var filter = require('../common/filter')
var utils = require('../common/utils')
var render = require('../common/render')

var db = require('../db')

var projectDao = wrap(db.get('api-project'))
var prdDao = wrap(db.get('api-prd'))
var apiDao = wrap(db.get('api-api'))


//项目列表
router.get('/project', function*(next) {
    var projects = yield projectDao.find(this.query, {
        sort: {
            updateTime: -1
        }
    });

    utils.success(this, projects);
});

//创建项目
router.post('/project/create', function*(next) {
    var body = yield parse(this)
    var name = body.name;
    var project = yield projectDao.findOne({
        name: name
    });

    if (!project) {
        project = {
            name: name,
            createTime: Date.now(),
            updateTime: Date.now()
        }

        project = yield projectDao.insert(project);
        utils.success(this, project);
    } else {
        utils.failed(this, 120001);
    }
});

//prd列表
router.get('/prd', function*(next) {
    var query = this.query
    var prds = yield prdDao.find(query, {
        sort: {
            updateTime: -1
        }
    });

    utils.success(this, prds);
});

//创建prd
router.post('/prd/create', function*(next) {
    var body = yield parse(this);
    var name = body.name;
    var prd = yield prdDao.findOne(body);

    if (!prd) {
        var tag = true;
        var v4 = '';
        while (tag) {
            v4 = uuid.v4().substr(0, 6);
            var exist = yield prdDao.findOne({
                uuid: v4
            });

            tag = !!exist;
        }

        prd = {
            uuid: v4,
            projectId: body.projectId,
            name: name,
            createTime: Date.now(),
            updateTime: Date.now()
        }

        prd = yield prdDao.insert(prd);
        utils.success(this, prd);
    } else {
        utils.failed(this, 120002);
    }
});

//api列表
router.get('/api', function*(next) {
    var query = this.query
    var apis = yield apiDao.find(query);

    utils.success(this, apis);
});

//创建api
router.post('/api/create', function*(next) {
    var body = yield parse(this);
    var name = body.name;

    if(body.isUpdate) {
        var api = yield apiDao.update({
            _id: body._id
        }, {
            $set: body
        });
        utils.success(this, body);
    }else{
        var api = yield apiDao.findOne({
            prdId: body.prdId,
            url: body.url
        });


        if (!api) {
            var api = utils.extend({
                    createTime: Date.now(),
                    updateTime: Date.now()
                }, body);

            api = yield apiDao.insert(api);
            utils.success(this, api);
        } else {
            utils.failed(this, 120003);
        }
    }
});

//api列表
router.get('/api/remove', function*(next) {
    var query = this.query
    var api = yield apiDao.remove(query);

    utils.success(this, api);
});

router.get('/debug/:prdId/(.*)', function *(next){
    var params = this.params;
    var prd = yield prdDao.findOne({
        uuid: params.prdId
    });

    this.set('Access-Control-Allow-Origin', '*');

    var url = params['0']
    if(prd && url){
        var api = yield apiDao.findOne({
            prdId: prd._id.toString(),
            url: '/' + url
        });

        console.log(api)

        this.body = api.output;
    }else{
        utils.failed(this, 120004);
    }
});


// api管理
router.get('/', function*(next) {
    /*switch (this.accepts('json', 'html', 'text')) {
        case 'json':
            utils.success({});
            break;
        default:
            this.body = yield render('api', utils.extend({
                title: 'api管理'
            }, this.locals));
            break;
    }*/
    this.body = yield render('api', utils.extend({
        title: 'api管理'
    }, this.locals));
}).get('/(.*)', function*(netx) {
    this.body = yield render('api', utils.extend({
        title: 'api管理'
    }, this.locals));
});

module.exports = router;
